package com.example.mybatis_plus_use.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.mybatis_plus_use.ValidationGroups.ValidationGroups.RegisterGroup;
import com.example.mybatis_plus_use.ValidationGroups.ValidationGroups.UpdateGroup;
import com.example.mybatis_plus_use.enums.Education;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.*;

import java.io.Serializable;
import java.util.List;
//在实体类中添加@TableField注解，指定数据库字段名
//在实体类中添加验证注解同时指定验证分组之后，在Controller层使用@Validated注解进行验证
@TableName("user")
@Getter
@Setter
@Builder//使用build要有无参构造函数
@NoArgsConstructor
@AllArgsConstructor
public class User extends BaseEntity {
    private static final long serialVersionUID = 1L;
    @Schema( description = "用户ID")
    @TableId(value = "id", type = IdType.AUTO)
    @NotNull(message = "ID不能为空", groups = UpdateGroup.class)
    private Integer id;

    @Schema(description = "用户名")
    @TableField("username")
    @NotBlank(message = "用户名不能为空", groups = {RegisterGroup.class, UpdateGroup.class})
    @Size(min = 2, max = 20, message = "用户名长度必须在2-20之间",
            groups = {RegisterGroup.class, UpdateGroup.class})
    private String username;
    @Schema(description = "邮箱")
    @TableField("email")
    @NotBlank(message = "邮箱不能为空", groups = {RegisterGroup.class, UpdateGroup.class})
    @Email(message = "邮箱格式不正确", groups = {RegisterGroup.class, UpdateGroup.class})
    private String email;
    @Schema(description = "密码")
    @TableField("password")
    @NotBlank(message = "密码不能为空", groups = RegisterGroup.class)
    @Pattern(
            regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{6,}$",
            message = "密码必须包含大小写字母和数字，长度至少8位",
            groups = RegisterGroup.class
    )
    private String password;
    @Schema(description = "年龄")
    @TableField("Age")
    @NotNull(message = "年龄不能为空", groups = {RegisterGroup.class, UpdateGroup.class})
    @Min(value = 0, message = "年龄不能小于0", groups = {RegisterGroup.class, UpdateGroup.class})
    @Max(value = 150, message = "年龄不能大于150", groups = {RegisterGroup.class, UpdateGroup.class})
    private Integer age;
    @Schema(description = "性别")
    @TableField("Sex")
    @NotBlank(message = "性别不能为空", groups = {RegisterGroup.class, UpdateGroup.class})
    @Pattern(regexp = "^(男|女)$", message = "性别只能是男或女",
            groups = {RegisterGroup.class, UpdateGroup.class})
    private String sex;
    @Schema(description = "学历")
    @TableField("education")
    private Education education;
    @Schema(description = "订单列表")
    @TableField(exist = false)
    private List<Orders> orderList;

    @Override
    public Serializable pkVal() {
        return this.id;
    }

}